nice_things/text/posix_character_classes.sh
The readonly variables in this module contain character range expressions that are equivalent to the named character classes specified in POSIX for use in shell expressions. These variables match exactly the minimum ASCII characters required for each character class in the POSIX locale as specified in POSIX.
When used directly in expressions, these variables should be expanded unquoted, ignoring shellcheck rule SC2295, since matching the resulting expansion as a pattern is exactly what we want. These character classes work on pathname expansion, on substring expansion, and on patterns in the matching arms of the case statement.
This module exists for compatibility purposes, because POSIX character classes are poorly supported or even not supported at all in historical versions of several shells, like dash and mksh. For maximum compatibility, you should always use these variables or explicit character ranges instead of named character classes.
Equivalence table
CLASS VARIABLE CHARACTERS
[[:alnum:]] = [${alnum}] = [0-9A-Za-z]
[[:alpha:]] = [${alpha}] = [A-Za-z]
[[:blank:]] = [${blank}] = [<space><tab>]
[[:cntrl:]] = [${cntrl}] = [\001-\037\177]
[[:digit:]] = [${digit}] = [0-9]
[[:graph:]] = [${graph}] = [B-~!-A]
[[:lower:]] = [${lower}] = [a-z]
[[:print:]] = [${print}] = [<space>-~]
[[:punct:]] = [${punct}] = [:-@[-`{-~!-/]
[[:space:]] = [${space}] = [\011-\015<space>]
[[:upper:]] = [${upper}] = [A-Z]
[[:xdigit:]] = [${xdigit}] = [0-9A-Fa-f]
readonly:alnum
Since 0.3.0 · Source
import "{ readonly:alnum }" from nice_things/text/posix_character_classes.sh
Synopsis[${alnum}]
Configuration
–
Description
Alphanumeric characters, matches all characters from the alpha and digit classes.
Equivalent to [0-9A-Za-z].
Options
–
Operands
–
Stdin
–
Stdout
–
Stderr
–
Exit status
–
Abort
–
Usage examples
is_name() {
case "${1?}" in "" | [!_${alpha}]* | *[!_${alnum}]*) return 1 ;; esac
}
readonly:alpha
Since 0.3.0 · Source
import "{ readonly:alpha }" from nice_things/text/posix_character_classes.sh
Synopsis[${alpha}]
Configuration
–
Description
Letter characters, matches all characters from the lower and upper classes.
Equivalent to [A-Za-z].
Options
–
Operands
–
Stdin
–
Stdout
–
Stderr
–
Exit status
–
Abort
–
Usage examples
is_name() {
case "${1?}" in "" | [!_${alpha}]* | *[!_${alnum}]*) return 1 ;; esac
}
readonly:blank
Since 0.3.0 · Source
import "{ readonly:blank }" from nice_things/text/posix_character_classes.sh
Synopsis[${blank}]
Configuration
–
Description
Blank characters, matches the <space> and <tab> characters.
Equivalent to [<space><tab>].
Options
–
Operands
–
Stdin
–
Stdout
–
Stderr
–
Exit status
–
Abort
–
Usage examples
line=${line#"${line%%[!${blank}]*}"} # Remove indentation
readonly:cntrl
Since 0.3.0 · Source
import "{ readonly:cntrl }" from nice_things/text/posix_character_classes.sh
Synopsis[${cntrl}]
Configuration
–
Description
Control characters. Matches all ASCII control characters, except the null byte (\000) is omitted since shells don't support null within strings.
In the ASCII character set, this class is the inverse of the print class, meaning all characters not in print are in cntrl.
Note that, apart from <space>, all other space characters are included in this class: <tab>, <line-feed>, <vertical-tab>, <form-feed>, <carriage-return>
Equivalent to [\001-\037\177] (in printf-style octal representation).
Options
–
Operands
–
Stdin
–
Stdout
–
Stderr
–
Exit status
–
Abort
–
Usage examples
case "$json_string" in *[${cntrl}]*)
log_error "Invalid JSON string. The string contains control characters."
;;
esac
readonly:digit
Since 0.3.0 · Source
import "{ readonly:digit }" from nice_things/text/posix_character_classes.sh
Synopsis[${digit}]
Configuration
–
Description
Number characters.
Equivalent to [0-9].
Options
–
Operands
–
Stdin
–
Stdout
–
Stderr
–
Exit status
–
Abort
–
Usage examples
is_decimal() {
case "${1?}" in "" | *[!.${digit}]* | 0[${digit}]* | *.*.*) return 1 ;; esac
}
readonly:graph
Since 0.3.0 · Source
import "{ readonly:graph }" from nice_things/text/posix_character_classes.sh
Synopsis[${graph}]
Configuration
–
Description
All printable characters (print), except <space>.
Equivalent to [B-~!-A].
Options
–
Operands
–
Stdin
–
Stdout
–
Stderr
–
Exit status
–
Abort
–
Usage examples
case "$text" in *[!${graph}]*)
log_info "Text contains non-graphical characters."
;;
esac
readonly:lower
Since 0.3.0 · Source
import "{ readonly:lower }" from nice_things/text/posix_character_classes.sh
Synopsis[${lower}]
Configuration
–
Description
Lowercase letters.
Equivalent to [a-z].
Options
–
Operands
–
Stdin
–
Stdout
–
Stderr
–
Exit status
–
Abort
–
Usage examples
is_lowercase() {
case "${1?}" in
*[${upper}]*) return 1 ;;
*[${lower}]*) ;;
*) return 1 ;;
esac
}
readonly:print
Since 0.3.0 · Source
import "{ readonly:print }" from nice_things/text/posix_character_classes.sh
Synopsis[${print}]
Configuration
–
Description
Printable characters.
In the ASCII character set, this class is the inverse of the cntrl class, meaning all characters not in cntrl are in print.
graph is a subset of this class, all characters from graph are also included in print.
Equivalent to [<space>-~].
Options
–
Operands
–
Stdin
–
Stdout
–
Stderr
–
Exit status
–
Abort
–
Usage examples
case "$text" in *[!${print}]*)
log_info "Text contains non-printable characters."
;;
esac
readonly:punct
Since 0.3.0 · Source
import "{ readonly:punct }" from nice_things/text/posix_character_classes.sh
Synopsis[${punct}]
Configuration
–
Description
Punctuation characters.
Equivalent to [:-@[-`{-~!-/].
Options
–
Operands
–
Stdin
–
Stdout
–
Stderr
–
Exit status
–
Abort
–
Usage examples
case "$text" in *[${punct}]*)
log_info "Text contains punctuation."
;;
esac
readonly:space
Since 0.3.0 · Source
import "{ readonly:space }" from nice_things/text/posix_character_classes.sh
Synopsis[${space}]
Configuration
–
Description
Space characters.
Note that, besides the <space> character, this class also matches several control characters: <tab>, <line-feed>, <vertical-tab>, <form-feed>, <carriage-return>. This is often not what one might expect. Most use-cases are better served by the blank class instead.
Equivalent to [\011-\015<space>] (in printf-style octal representation).
Options
–
Operands
–
Stdin
–
Stdout
–
Stderr
–
Exit status
–
Abort
–
Usage examples
case "$text" in *[${space}]*)
log_info "Text contains space characters."
;;
esac
readonly:upper
Since 0.3.0 · Source
import "{ readonly:upper }" from nice_things/text/posix_character_classes.sh
Synopsis[${upper}]
Configuration
–
Description
Uppercase letters.
Equivalent to [A-Z].
Options
–
Operands
–
Stdin
–
Stdout
–
Stderr
–
Exit status
–
Abort
–
Usage examples
is_all_caps() {
case "${1?}" in
*[${lower}]*) return 1 ;;
*[${upper}]*) ;;
*) return 1 ;;
esac
}
readonly:xdigit
Since 0.3.0 · Source
import "{ readonly:xdigit }" from nice_things/text/posix_character_classes.sh
Synopsis[${xdigit}]
Configuration
–
Description
Hexadecimal digits.
Equivalent to [0-9A-Fa-f].
Options
–
Operands
–
Stdin
–
Stdout
–
Stderr
–
Exit status
–
Abort
–
Usage examples
is_hexadecimal() {
case "${1?}" in
"" | 0x | 0x*[!${xdigit}]*) return 1 ;;
0x*) ;;
*[!${xdigit}]*) return 1 ;;
esac
}